'use strict';

const { Service } = require('egg');

class MembersService extends Service {
  async list()  {
    // const token = this.ctx.request.header.authorization;
    let { pageSize, page } = this.ctx.request.query;
    pageSize = pageSize || 20;
    page = page || 0;
    const memberList = await this.ctx.model.Members.findAll({
      limit: pageSize,
      offset: page
    })
    return memberList;
  }

  /* 注册新用户 */
  async createOne() {
    const { ctx, app } = this;
    let { name, password, ...rest } = ctx.request.body;
    const newMember = await ctx.model.Members.create({ name, password, ...rest })
    return newMember;
  }

  /* 生成token */
  async getToken() {
    const { ctx, app } = this;
    const data = ctx.request.body;
    const token = app.jwt.sign({
      username: data.username,
      role: data.role,
      uuid: data.uuid
    }, app.config.jwt.secret);
    // await app.redis.set(data.username, token);
    return token;
  }

  /* 验证token */
  async checkInfo() {
    const { ctx } = this;
    const { user} = ctx.state;
    return user
  }
}

module.exports = MembersService;